﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using BHair.Business;
using BHair.Business.Table;
using BHair.Report;
using BHair.Base;
using BHair.bb;
using CCWin;
namespace BHair
{
    public partial class frmxmmx : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmxmmx()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            /*string sql1 = "";
            SqlConnection conn1 = new SqlConnection(constr);*/
           // timeselect();
            //判断卡号是否为空
           /* if (this.txtcardid.Text != string.Empty)
            {
                cardxf();
            }
            else if (this.txtcardid.Text == string.Empty)//如果卡号为空
            {
                timeselect();
            }
            else 
            {
                xmselect();
            }
            * */
            if (checkBox1.Checked)//如果选择按时间统计
            {
                
                timeselect();
            }
            else if (checkBox2.Checked)//如果选择按时间统计单个项目
            {
                
                selecttimexm();
            }
            else if (checkBox3.Checked)//按卡号查询
            {
                
                cardxf();
            }
            else                            //查询所有
            {
                selectsy();
            }

        }
        //加载下拉框
        public void com() 
        {
            string sql2 = "select spname from jc";
            
            SqlConnection conn2 = new SqlConnection(constr);
            try
            {
                conn2.Open();
                SqlDataAdapter sda1 = new SqlDataAdapter(sql2, conn2);
                DataSet ds1 = new DataSet();
                sda1.Fill(ds1, "jc");
                this.comboBox1.DataSource = ds1.Tables["jc"];
                this.comboBox1.DisplayMember = "spname";
                conn2.Close();
            }
            catch (Exception e1) 
            {
                MessageBox.Show(e1.Message);
            }
        }
        public void selectsy() 
        {
            SqlConnection conn = new SqlConnection(constr);
            string sql = "select jcxf.cardid as 卡号,jcxf.djbh as 单据号,jc.spname as 项目名称,jcxf.xfcs as 消费次数,jcxf.riqi as 消费日期 from jcxf inner join jc on jcxf.xmid=jc.id";
            try 
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                this.dataGridView1.DataSource = dt;
                conn.Close();

            }catch(Exception e6)
            {
                MessageBox.Show(e6.Message);
            }
        }
        public void timeselect() 
        {
            SqlConnection conn4 = new SqlConnection(constr);
            string riqi = this.dtpriqi.Text;
            //MessageBox.Show(riqi);
            string sqltime =string.Format( "select jcxf.cardid as 卡号,jcxf.djbh as 单据号,jc.spname as 项目名称,jcxf.xfcs as 消费次数,jcxf.riqi as 消费日期 from jcxf inner join jc on jcxf.xmid=jc.id where jcxf.riqi='{0}'",riqi);
            try 
            {
                conn4.Open();
                SqlDataAdapter sda2 = new SqlDataAdapter(sqltime,conn4);
                DataTable dt1 = new DataTable();
                sda2.Fill(dt1);
                this.dataGridView1.DataSource = dt1;
                conn4.Close();
            }catch(Exception e2)
            {
                conn4.Close();
                MessageBox.Show(e2.Message);
            }
        }
        //按卡号查询
        public void cardxf() 
        {
            SqlConnection conn5 = new SqlConnection(constr);

            string sql3 = string.Format("select jcxf.cardid as 卡号,jcxf.djbh as 单据号,jc.spname as 项目名称,jcxf.xfcs as 消费次数,jcxf.riqi as 消费日期 from jcxf inner join jc on jcxf.xmid=jc.id where jcxf.cardid='{0}' and jcxf.riqi='{1}'",this.txtcardid.Text,this.dtpriqi.Text);
            try
            {
                conn5.Open();
                SqlDataAdapter sda3 = new SqlDataAdapter(sql3, conn5);
                DataTable dt2 = new DataTable();
                //DataSet ds = new DataSet();
               // sda3.Fill(ds,"jcxf");
                sda3.Fill(dt2);
                this.dataGridView1.DataSource = dt2;
                
                /*CrystalDecisions.CrystalReports.Engine.ReportDocument re = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                string redatasous=@"E:\开发2017\美发软件\hygl2017-03-121\hygl2017-03-12\hygl\BHair\crjcxfmx.rpt";
                re.Load(redatasous);
                re.SetDataSource(ds);
                 * */
                conn5.Close();
            }
            catch (Exception e3)
            {
                conn5.Close();
                MessageBox.Show(e3.Message);
            }

        
        }
        //按时间统计单个项目
        public void selecttimexm() 
        {
            SqlConnection connsx = new SqlConnection(constr);
            string sqlsx =string.Format( "select jcxf.cardid as 卡号,jcxf.djbh as 单据号,jc.spname as 项目名称,jcxf.xfcs as 消费次数,jcxf.riqi as 消费日期 from jcxf inner join jc on jcxf.xmid=jc.id where jc.spname='{0}' and jcxf.riqi='{1}'",this.comboBox1.Text,this.dtpriqi.Text);
            try
            {
                connsx.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sqlsx, connsx);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                this.dataGridView1.DataSource = dt;
                connsx.Close();
            }
            catch (Exception ex6) 
            {
                connsx.Close();
                MessageBox.Show(ex6.Message);
            }
        }
        public void xmselect() 
        {
            string sql5 =string.Format( "select jcxf.cardid as 卡号,jcxf.djbh as 单据号,jc.spname as 项目名称,jcxf.xfcs as 消费次数,jcxf.riqi as 消费日期 from jcxf inner join jc on jcxf.xmid=jc.id where jc.spname='{0}'",this.comboBox1.Text);
            SqlConnection conn6 = new SqlConnection(constr);
            try
            {
                conn6.Open();
                SqlDataAdapter sda4 = new SqlDataAdapter(sql5,conn6);
                DataTable dt4 = new DataTable();
                sda4.Fill(dt4);
                this.dataGridView1.DataSource = dt4;
                conn6.Close();
            }
            catch (Exception e4)
            {
                conn6.Close();
                MessageBox.Show(e4.Message);
            }
        }

        private void frmxmmx_Load(object sender, EventArgs e)
        {
            com();
            //this.reportViewer1.RefreshReport();
        }
        GridPrinter gridPrinter;
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

            if (gridPrinter == null)
            {

                gridPrinter = new GridPrinter(dataGridView1, printDocument1, true, true, "报表", dataGridView1.DefaultCellStyle.Font, Color.Black, true);

            }

            bool more = gridPrinter.DrawDataGridView(e.Graphics);

            if (more == true)

                e.HasMorePages = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            printDialog1.Document = this.printDocument1;
            DialogResult result = printDialog1.ShowDialog();

            // If the result is OK then print the document.

            if (result == DialogResult.OK)
            {
                printPreviewDialog1.Document = this.printDocument1; //设置预览文档

                printPreviewDialog1.UseAntiAlias = true; //设置UseAntiAlias属性为true，开启防锯齿功能

                printPreviewDialog1.ShowDialog();//使用ShowDialog方法，显示预览窗口
            }
        }
        /*
         * 设置如果选中任意按钮则其他按钮变成未选中状态
         * */
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            checkBox2.Checked = false;
            checkBox3.Checked = false;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox3.Checked = false;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string cardid = this.txtcardid.Text;
            string kstime = this.dtpriqi.Text;
            //string endtime = this.dtpend.Text;
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT * FROM [BHairDB].[dbo].[jc]";
            SqlDataAdapter sda1 = new SqlDataAdapter(sql, conn);
            DataSet ds1 = new DataSet();
            sda1.Fill(ds1, "jc");
            string sql2 = string.Format("SELECT [id],[djbh],[xmid],[xmname],[xfcs],[riqi],[cardid] FROM [BHairDB].[dbo].[jcxf] where cardid='{0}' and '{1}'=riqi", cardid, kstime);
            SqlDataAdapter sda2 = new SqlDataAdapter(sql2, conn);
            sda2.Fill(ds1, "jcxf");
           /* jcxfmx jcxf = new jcxfmx();
            jcxf.SetDataSource(ds1);
            this.crystalReportViewer1.ReportSource = jcxf;
            //this.crystalReportViewer1.PrintReport();
            //this.crystalReportViewer1.ShowPreviousPage();
            this.crystalReportViewer1.ExportReport();
            * */
        }

        

        
    }
}
